package controllers.participant;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.ejb.criteria.expression.function.CurrentDateFunction;

import com.google.appengine.api.datastore.Blob;

import play.data.Upload;
import play.data.validation.Valid;
import models.Focus;
import models.InformationRequest;
import models.InformationUnit;
import models.UploadFile;
import models.User;

public class InformationRequestController extends ParticipantBaseController {
	
	/**
	 * 
	 * @param id The information request.
	 */
	public static void form(Long id) {
		InformationRequest information = InformationRequest.getById(id, null);
		// TODO validate that this information request is available
		InformationUnit unit = new InformationUnit();
		
		render(information, unit);
	}
	
	/**
	 * 
	 * @param id The information request.
	 * @param unit 
	 */
	public static void save(Long id, InformationUnit unit, Upload[] mediaFile) {
		InformationRequest information = InformationRequest.getById(id, null);
		unit.author = getCurrentUser();
		unit.informationRequest = information; // TODO validate that this information request is available
		unit.created = new Date();
		
		unit.save();
		
		if(mediaFile != null) {
			for(Upload media : mediaFile) {
				if(media.getSize() > 0) {
					UploadFile file = new UploadFile();
					file.name = media.getFileName();
					file.type = media.getContentType();
					file.blob = new Blob(media.asBytes());
					file.unit = unit;
					file.save();
				}
			}
			//render("participant/informationRequestController/thanks.html",unit);
		}
		controllers.participant.InformationRequestController.thanks(unit.id);
	}
	
	public static void thanks(Long id) {
		InformationUnit unit = InformationUnit.findById(id);
		render("participant/InformationRequestController/thanks.html",unit);
	}
}
